<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 待处理售后订单订单
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-delete"
            class="handle-del mr10"
            @click="delAllSelection"
        >批量处理</el-button>
        <el-select value="" placeholder="售后状态" class="handle-input mr10" v-model="pagination.status">
          <el-option label="全部" value=""></el-option>
          <el-option label="待处理" value="0"></el-option>
          <el-option label="已同意" value="1"></el-option>
          <el-option label="已拒绝" value="2"></el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="orderNo" label="订单编号"></el-table-column>
        <el-table-column prop="good_id" label="退款商品编号"></el-table-column>
        <el-table-column prop="service_reason" label="售后原因"></el-table-column>
        <el-table-column prop="service_way" label="服务方式"></el-table-column>
        <el-table-column prop="insurance_name" label="保险名称"></el-table-column>
        <el-table-column prop="refund_amount" label="退款金额"></el-table-column>
        <el-table-column prop="express" label="快递单号"></el-table-column>
        <el-table-column prop="returnAddress" label="寄回地址"></el-table-column>
        <el-table-column prop="status" label="订单状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="danger">待处理</el-tag>
            <el-tag v-if="scope.row.status === 1" type="info">已同意</el-tag>
            <el-tag v-if="scope.row.status === 2" type="warning">已拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
                       type="text"
                       icon="el-icon-edit"
                       @click="handleCheck(scope.$index, scope.row)"
            >查看详细信息</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
            background
            @current-change="handlePageChange"
            layout="total, prev, pager, next"
            :current-page="pagination.currentPage"
            :page-size="pagination.pageSize"
            :total="pagination.total">
          <!--:total="pageTotal"-->
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="售后详情" :visible.sync="editVisible" width="35%">
      <el-form ref="form" :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input v-model="form.orderNo" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款商品编号" prop="good_id">
              <el-input v-model="form.good_id" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="售后原因" prop="service_reason">
              <el-input v-model="form.service_reason" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务方式" prop="service_way">
              <el-input v-model="form.service_way" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保险名称" prop="insurance_name">
              <el-input v-model="form.insurance_name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额" prop="refund_amount">
              <el-input v-model="form.refund_amount" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="快递单号" prop="express">
              <el-input v-model="form.express" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="寄回地址" prop="returnAddress">
              <el-input v-model="form.returnAddress" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="退款说明" prop="refund_explain">
              <el-input
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 4}"
                  placeholder="无"
                  v-model="form.instructions" disabled>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="退款说明图片">
              <el-image v-if="form.instructions_img" :src="form.instructions_img"></el-image>
              <span v-else>无</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="24">
            <el-form-item label="拒绝理由">
              <el-input
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 4}"
                  placeholder="无"
                  v-model="form.store_reply">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="agreeEdit" v-if="form.status === 0">同 意</el-button>
                <el-button type="primary" @click="agreeEdit" v-if="form.status !== 0" disabled>同 意</el-button>
                <el-button type="danger" @click="rejectEdit" v-if="form.status === 0">拒 绝</el-button>
                <el-button type="danger" @click="rejectEdit" v-if="form.status !== 0" disabled>拒 绝</el-button>
                <el-button @click="editVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  editOrderStatusById,
  fetchData,
  getServiceOrderAll, getServiceOrderByStatusAndStoreId,
  getServiceOrderByStoreId,
  getStoreIdByName
} from '../../api/index';
import { getUsers,getOrders,getServiceOrder,updateServiceOrderById } from '../../api';
export default {
  name: 'WaitShip',
  data() {
    return {
      pagination: {
        status: '',
        store_id: '',
        currentPage: 0,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      formLabelWidth: '120px',
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      if (localStorage.ms_username == 'admin') {
        var params = {
          pageIndex: this.pagination.currentPage,
          pageSize: this.pagination.pageSize
        };
        getServiceOrder(params).then(res => {
          if (res.status == 200) {
            this.tableData = res.list;
            this.pagination.total = res.total;
          }else {
            this.$message.error(res.msg);
          }
        });
      }else {
        this.pagination.store_id = localStorage.getItem("storeId");
        var params = {
          pageIndex: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          store_id: this.pagination.store_id,
        };
        getServiceOrderByStoreId(params).then(res => {
          if (res.status == 200) {
            this.tableData = res.list;
            this.pagination.total = res.total;
          }else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    // 触发搜索按钮
    handleSearch() {
      if (this.pagination.status){
        this.pagination.store_id = localStorage.getItem("storeId");
        var params = {
          pageIndex: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          status: this.pagination.status,
          store_id: this.pagination.store_id,
        };
        getServiceOrderByStatusAndStoreId(params).then(res => {
          if (res.status == 200) {
            this.tableData = res.list;
            this.pagination.total = res.total;
          }else {
            this.$message.error(res.msg);
          }
        });
      }else {
        this.pagination.store_id = localStorage.getItem("storeId");
        var params = {
          pageIndex: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          status: this.pagination.status,
          store_id: this.pagination.store_id,
        };
        getServiceOrderAll(params).then(res => {
          if (res.status == 200) {
            this.tableData = res.list;
            this.pagination.total = res.total;
          }else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
          .then(() => {
            this.$message.success('删除成功');
            this.tableData.splice(index, 1);
          })
          .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleCheck(index, row) {
      this.editVisible = true;
      this.form = row;
    },
    // 同意申请
    agreeEdit() {
      this.form.status = 1;
      updateServiceOrderById(this.form).then(res => {
        if (res.status == 200) {
          this.$message.success('操作成功');
          this.getData();
        }else {
          this.$message.error(res.msg);
        }
      });
      this.editVisible = false;
      this.getData();
    },
    // 拒绝申请
    rejectEdit() {
      this.form.status = 2;
      if(this.form.store_reply){
        updateServiceOrderById(this.form).then(res => {
          if (res.status == 200) {
            this.$message.success('操作成功');
            this.getData();
          }else {
            this.$message.error(res.msg);
          }
        });
        this.editVisible = false;
      }else {
        this.$message.error('请填写拒绝原因');
      }
      this.getData();
    },
    // 分页导航
    handlePageChange(val) {
      this.pagination.currentPage = val;
      this.getData();
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 200px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
